﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using XWord.Fundamental.Domain;

namespace XWord.GUI.Views
{
    /// <summary>
    /// Interaction logic for WordSearchView.xaml
    /// </summary>
    public partial class WordFinderView : Window
    {
        public WordFinderView()
        {
            InitializeComponent();
          
            this.SetBinding(WordItemsProperty, new Binding("WordItems"));
        }

        public IList<Word> WordItems
        {
            get { return (IList<Word>)GetValue(WordItemsProperty); }
            set { SetValue(WordItemsProperty, value); }
        }

        
        public static readonly DependencyProperty WordItemsProperty =
            DependencyProperty.Register("WordItems", typeof(IList<Word>), typeof(WordFinderView), 
            new UIPropertyMetadata(null));

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            CollectionViewSource.GetDefaultView(WordItems).Filter = new Predicate<object>((obj) =>
            {
                var entity = obj as Word;
                var boolList = new List<bool>();

                if (!string.IsNullOrEmpty(word_key.Text))
                {
                    if (prefix.IsChecked.Value)
                    {
                        boolList.Add(entity.Name.StartsWith(word_key.Text));
                    }
                    else
                    {
                        boolList.Add(entity.Name.Contains(word_key.Text));
                    }
                }
                if (!string.IsNullOrEmpty(word_chinaKey.Text))
                {
                    boolList.Add(entity.Comment.Contains(word_chinaKey.Text));
                }

                if (suffix.IsChecked.Value)
                {
                    boolList.Add(entity.Name.EndsWith(word_key.Text));
                }
                if (checkBox1.IsChecked.Value)
                {
                    boolList.Add(string.IsNullOrEmpty(entity.Suffix));
                }

                if (!string.IsNullOrEmpty(word_length.Text))
                {
                    boolList.Add(entity.Name.Length == Convert.ToInt32(word_length.Text));
                }
                return !boolList.Contains(false);
            });
        }
    }
}
